package com.example.teadoctor;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import java.util.ArrayList;
import java.util.Objects;

public class Search extends AppCompatActivity implements SearchView.OnQueryTextListener{
    private SearchView searchView;
    String id,name,year,image,type,date,record,description;
    //存放要跳转时传递给下一个页面的值，name、image、description
    String data;
    ArrayList<String> dataList;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_search);

        searchView = findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(this);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // 执行模糊查询操作
        fuzzySearch(newText);
        return true;
    }



    //执行模糊匹配
    private void fuzzySearch(String keyword) {
        // 在茶博士App的数据中执行模糊匹配搜索
        // 获取TeaDBHelper对象
        TeaDBHelper dbHelper = new TeaDBHelper(this);

        // 获取SQLiteDatabase对象
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        //通过SQL语句实现模糊匹配
        String query = "SELECT * FROM tea_info WHERE name LIKE '%" + keyword + "%' OR year LIKE '%" + keyword + "%'";
        Cursor cursor = db.rawQuery(query, null);
        dataList = new ArrayList<>();
        while(cursor.moveToNext()) {
            name = cursor.getString(cursor.getColumnIndex("name"));
            image = cursor.getString(cursor.getColumnIndex("image"));
            year = cursor.getString(cursor.getColumnIndex("year"));
            String record = "   茶名：" + name+ "     年份：" + year + "  " ;
            dataList.add(record);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(Objects.requireNonNull(this), android.R.layout.simple_list_item_1, dataList);
        listView = findViewById(R.id.Pst_my_search);
        listView.setAdapter(adapter);
        cursor.close();
        db.close();

        //点击记录跳转，要传递数据过去，具体展示
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedRecord = (String) parent.getItemAtPosition(position); // 获取所选中的记录
                // 解析记录中的数据
                name = selectedRecord.substring(selectedRecord.indexOf("茶名：") + 3, selectedRecord.indexOf("年份：")).trim();

                // 将茶名通过intent传递给要跳转的页面
                Intent intent = new Intent(Search.this, Information.class);
                intent.putExtra("name", name);
                startActivity(intent);
            }
        });
        // 将搜索结果展示在界面上
        adapter.notifyDataSetChanged();
    }
}

